package com.derbysoft.lowcostadapter.web.action.admin.cachemanager.search;

import com.derbysoft.lowcostadapter.commons.model.Hotel;
import com.derbysoft.lowcostadapter.commons.redis.service.HotelCacheService;
import com.derbysoft.lowcostadapter.web.action.BaseAdapterAction;
import com.derbysoft.lowcostadapter.web.action.model.HotelCondition;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by: jason
 * Date: 2012-09-26
 */
public class HotelSearchAction extends BaseAdapterAction {

    private HotelCondition hotelCondition;

    @Autowired
    private HotelCacheService hotelCacheService;

    @Action("searchHotel")
    public void searchHotel() {
        try {
            hotelCondition.validate();
            Hotel hotel = loadHotel();
            populateSucceededJsonResult(hotel != null ? hotel.value() : NOT_FOUND);
        } catch (Exception e) {
            populateFailedJsonResult(e);
        }
    }

    private Hotel loadHotel() {
        if (StringUtils.isNotBlank(hotelCondition.getPropertyId())) {
            return hotelCacheService.loadById(hotelCondition.getPropertyId());
        }
        return hotelCacheService.loadByPassport(hotelCondition.getPassport());
    }

    public HotelCondition getHotelCondition() {
        return hotelCondition;
    }

    public void setHotelCondition(HotelCondition hotelCondition) {
        this.hotelCondition = hotelCondition;
    }
}
